// 初始卡片
const initialData = [{
        title: "雅思考试分数记录",
        active: true,
        backgroundUrl: "考试卡.jpg",
        cardClassify: "preset",
        detailUrl: "",
        cardType: "all",
        needScore: true
    },
    {
        title: "听力考试分数记录",
        active: true,
        backgroundUrl: "考试卡.jpg",
        cardClassify: "preset",
        detailUrl: "",
        cardType: "listening",
        needScore: true
    },
    {
        title: "口语考试分数记录",
        active: true,
        backgroundUrl: "考试卡.jpg",
        cardClassify: "preset",
        detailUrl: "",
        cardType: "speaking",
        needScore: true
    },
    {
        title: "阅读考试分数记录",
        active: true,
        backgroundUrl: "考试卡.jpg",
        cardClassify: "preset",
        detailUrl: "",
        cardType: "reading",
        needScore: true
    },
    {
        title: "写作考试分数记录",
        active: true,
        backgroundUrl: "考试卡.jpg",
        cardClassify: "preset",
        detailUrl: "",
        cardType: "writing",
        needScore: true
    },
    {
        title: "剑桥雅思阅读考点词真经",
        active: true,
        backgroundUrl: "剑14版阅读考点词真经-1.jpg",
        cardClassify: "preset",
        detailUrl: "su4hGHuiB7bmWKZzBbgzyj4WbmMBrNCwTFhPQkGsWAT8BqbD",
        cardType: "reading",
        needScore: false,
        rectangleCover: true
    },
    {
        title: "雅思阅读真经总纲",
        active: true,
        backgroundUrl: "剑14版雅思阅读真经总纲-1.jpg",
        cardClassify: "preset",
        detailUrl: "2CkE40Q34TONBukjrZIz5TQQ5AWKCfijkIYpfjwqGTrW7MG8",
        cardType: "reading",
        needScore: false,
        rectangleCover: true
    },
    {
        title: "九分达人雅思阅读真题还原及解析6",
        active: true,
        backgroundUrl: "default_cover.jpg",
        cardClassify: "preset",
        detailUrl: "4IAtlIEEF6VxTobe3bIUgKbPG5VUzuhuSzFppnlJc7oAR7oB",
        cardType: "reading",
        needScore: false,
        rectangleCover: true
    },
    {
        title: "Simon写作",
        active: true,
        backgroundUrl: "simon.jpeg",
        cardClassify: "preset",
        detailUrl: "gVkffIDm0yz4dldg351eCttVaBq082eOgckXJgFy6l0T6qNL",
        cardType: "writing",
        needScore: false,
        rectangleCover: false
    },
    {
        title: "S王陆807雅思词汇精讲听力篇",
        active: true,
        backgroundUrl: "王陆807雅思词汇精讲听力篇.jpg",
        cardClassify: "preset",
        detailUrl: "C4LG43pAgzQYNU2Fk6Cj2m9sDShoatQ0nkbGRXdffd57pQIv",
        cardType: "listening",
        needScore: false,
        rectangleCover: true
    },
    {
        title: "九分达人雅思口语真题还原及解析2",
        active: true,
        backgroundUrl: "9分达人雅思口语真题还原及解析.jpg",
        cardClassify: "preset",
        detailUrl: "qz01hOL1HPJilVJUrEGhQ8K3EQZ365h2igo27VmdZTEhZaRO",
        cardType: "speaking",
        needScore: false,
        rectangleCover: true
    }
];

const cloud = require('wx-server-sdk');
// 初始化 cloud
cloud.init({
    // API 调用都保持和云函数当前所在环境一致
    env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database();
const _ = db.command;


/**
 * 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端
 * 
 * event 参数包含小程序端调用传入的 data
 * 
 */
exports.main = async(event, context) => {
    const response = {};
    const { OPENID } = cloud.getWXContext();
    const user = await db.collection('users')
        .where({
            openid: _.eq(OPENID)
        })
        .get();
    if (user.data && user.data.length) {
        response.code = 500;
        response.error = '用户已存在'
    } else {
        for (let i = 0; i < initialData.length; i++) {
            Object.assign(initialData[i], { openid: OPENID, created_time: new Date, updated_time: new Date });
            await db.collection('cards')
                .add({
                    data: initialData[i]
                })
                .then(() => {
                    response.code = 200;
                })
                .catch((err) => {
                    response.code = 500;
                    response.error = err;
                })
        }
    }
    return response;
}